package ytdp.careagle.com.ytdp.myview;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wx.wheelview.widget.WheelView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.adapter.MyCityAdapter;
import ytdp.careagle.com.ytdp.adapter.MyProvinceAdapter;
import ytdp.careagle.com.ytdp.entity.ProvinceEntity;

/**
 * Created by admin on 2017/10/17.
 */

public class SelectAddressView extends RelativeLayout implements WheelView.OnWheelItemSelectedListener {
    @Bind(R.id.tv_cancel)
    TextView tvCancel;
    @Bind(R.id.tv_ok)
    TextView tvOk;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.main_wheelview)
    WheelView provinceWheelview;
    @Bind(R.id.sub_wheelview)
    WheelView cityWheelview;
    @Bind(R.id.ll_content)
    LinearLayout llContent;
    @Bind(R.id.ll_viewGroup)
    LinearLayout llViewGroup;
    @Bind(R.id.ll_gen)
    RelativeLayout llGen;
    private Activity activity;
    private SelecedListener listener;
    private int selectProvinceIndex = 0;
    private int selectCityIndex = 0;
    private boolean hasCityAndIsFirst = false;
    private List<ProvinceEntity> provinceList;

    public SelectAddressView(Context context) {
        super(context);
    }

    public SelectAddressView(Context context, String provinceName, String cityName, List<ProvinceEntity> data) {
        super(context);
        activity = (Activity) context;
        View inflate = LayoutInflater.from(context).inflate(R.layout.layout_select_time, null);
        addView(inflate);
        ButterKnife.bind(this);
        provinceList = data;
        initView(provinceName, cityName);
        tvTitle.setText("车辆所在地");
    }

    @OnClick({R.id.tv_cancel, R.id.tv_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_ok:
                selectOk();
                closed();
                break;
            case R.id.tv_cancel:
                closed();
                break;
        }
    }

    private void selectOk() {
        ProvinceEntity provinceEntity = (ProvinceEntity) provinceWheelview.getSelectionItem();
        ProvinceEntity.ChildrenBean childrenBean = (ProvinceEntity.ChildrenBean) cityWheelview.getSelectionItem();
        if (listener != null) {
            listener.selected(provinceEntity.getProvinceName(), provinceEntity.getProvinceCode(),
                    childrenBean.getCityName(), childrenBean.getCityCode());
        }
    }

    public void closed() {
        animate().translationYBy(getTranslationY()).translationY(getHeight() * 2).setDuration(200).start();
    }

    public void open() {
        animate().translationYBy(getTranslationY()).translationY(0).setDuration(200).start();
    }

    public void initView(String provinceName, String cityName) {
        if (!TextUtils.isEmpty(provinceName)) {
            for (int i = 0; i < provinceList.size(); i++) {
                if (provinceList.get(i).getProvinceName().equals(provinceName)) {
                    selectProvinceIndex = i;
                    break;
                }
            }
        }
        if (!TextUtils.isEmpty(cityName)) {
            List<ProvinceEntity.ChildrenBean> children = provinceList.get(selectProvinceIndex).getChildren();
            for (int i = 0; i < children.size(); i++) {
                if (cityName.equals(children.get(i).getCityName())) {
                    selectCityIndex = i;
                    hasCityAndIsFirst = true;
                    break;
                }
            }
        }
        provinceWheelview.setWheelAdapter(new MyProvinceAdapter(activity));
        provinceWheelview.setWheelSize(7);
        provinceWheelview.setSkin(WheelView.Skin.Holo);
        provinceWheelview.setWheelData(provinceList);
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextSize = 30;
        style.textSize = 24;
        provinceWheelview.setStyle(style);

        cityWheelview.setWheelAdapter(new MyCityAdapter(activity));
        cityWheelview.setWheelSize(7);
        cityWheelview.setSkin(WheelView.Skin.Holo);
        List<ProvinceEntity.ChildrenBean> childrens = provinceList.get(selectProvinceIndex).getChildren();
        cityWheelview.setWheelData(childrens);
        cityWheelview.setStyle(style);
        provinceWheelview.setOnWheelItemSelectedListener(this);
        if (selectProvinceIndex != 0) {
            provinceWheelview.setSelection(selectProvinceIndex);
        }
        if (selectCityIndex != 0) {
            cityWheelview.setSelection(selectCityIndex);
        }
        open();
    }

    public void setListener(SelecedListener listener) {
        this.listener = listener;
    }

    @Override
    public void onItemSelected(int position, Object o) {
        if (hasCityAndIsFirst) {
            hasCityAndIsFirst = !hasCityAndIsFirst;
        } else {
            cityWheelview.setWheelData(provinceList.get(position).getChildren());
        }
    }

    public interface SelecedListener {
        void selected(String provinceName, String provinceCode, String cityName, String cityCode);
    }
}
